package fr.teuteuf.runstickmanrun.map.tile;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import fr.teuteuf.runstickmanrun.Constants;
import fr.teuteuf.runstickmanrun.IUpdatable;

public class MovingSolidBoxTile extends SolidBoxTile implements IUpdatable {
	
	private Vector2 initialPosition;
	private Vector2 movingDistance;

	public MovingSolidBoxTile(byte borders, int textureX, int textureY, int positionX, int positionY, float velX, float velY, float dX, float dY, World world) {
		super(borders, textureX, textureY, positionX, positionY, world);
		initialPosition = getBody().getPosition().cpy();
		getBody().setType(BodyType.KinematicBody);
		getBody().setLinearVelocity(new Vector2(velX, velY));
		movingDistance = new Vector2(dX * Constants.WORLD_TO_BOX, dY * Constants.WORLD_TO_BOX);
	}

	@Override
	public void update(float elapsedTime) {
		if(getBody().getLinearVelocity().x > 0 && getBody().getPosition().x > initialPosition.x + movingDistance.x
		|| getBody().getLinearVelocity().x < 0 && getBody().getPosition().x < initialPosition.x) {
			getBody().setLinearVelocity(getBody().getLinearVelocity().x * -1f, getBody().getLinearVelocity().y);
		}
		
		if(getBody().getLinearVelocity().y > 0 && getBody().getPosition().y > initialPosition.y + movingDistance.y
		|| getBody().getLinearVelocity().y < 0 && getBody().getPosition().y < initialPosition.y) {
			getBody().setLinearVelocity(getBody().getLinearVelocity().x, getBody().getLinearVelocity().y * -1f);
		}
		
		position.set(getBody().getPosition().x * Constants.BOX_TO_WORLD - TILE_SIZE/2, getBody().getPosition().y * Constants.BOX_TO_WORLD - TILE_SIZE/2);
	}
}
